package com.bigegg.employee.service.impl;

import com.bigegg.employee.domain.*;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.bigegg.employee.service.TypeUserService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 学生端用户类型
 *
 * @author sun
 */
@Component
@Order(2)
public class TypeUser2Service implements TypeUserService {


    public static final String USER_ROLE = "学生";

    public static final int USER_TYPE = 2;

    @Autowired
    CRoleService cRoleService;

    @Autowired
    SysUserServiceImpl userService;


    @Override
    public int getType() {
        return USER_TYPE;
    }

    @Override
    public String getTypeName() {
        return USER_ROLE;
    }

    @Override
    public boolean support(String role) {
        return USER_ROLE.equals(role);
    }

    @Override
    public void register(RegisterInfo registerInfo) {
        SysUser user = registerInfo.parse2User();
        SysRole role = cRoleService.getRoleByName(USER_ROLE);
        if (role == null) {
            throw new RuntimeException("无法查找到 [" + USER_ROLE + "] 的角色，确保系统初始化正确！");
        }
        user.setRoleIds(new Long[]{role.getRoleId()});
        validateUser(user);
        userService.insertUser(user);
    }

    private void validateUser(SysUser user) {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) {
            throw new RuntimeException("注册用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (user.getPhonenumber() != null && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            throw new RuntimeException("注册用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (user.getEmail() != null && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            throw new RuntimeException("注册用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
    }


    @Autowired
    GraduateServiceImpl graduateService;

    /**
     * 查询学生基本信息
     *
     * @param user 登录用户
     * @return 辅导员信息
     */
    @Override
    public TypeUserInfo getLoginUserInfo(SysUser user) {
        Graduate graduate = new Graduate();
        graduate.setStuNo(user.getUserName());
        // 获取毕业生信息和用户名一致的数据
        List<Graduate> graduateList = graduateService.selectGraduateList(graduate);
        if (graduateList == null || graduateList.size() < 1) {
            // 没找到则直接返回
            return new TypeStudentInfo(user);
        }
        // 赋值毕业年份和学院、专业
        Graduate graduateOne = graduateList.get(0);
        return getStudentInfo(graduateOne, user);
    }

    private TypeStudentInfo getStudentInfo(Graduate graduate, SysUser user) {
        TypeStudentInfo studentInfo = new TypeStudentInfo(user);
        studentInfo.setStudentId(graduate.getId());
        studentInfo.setGraduateSchool(graduate.getGraduateSchool());
        studentInfo.setDepartment(graduate.getCollegeName());
        studentInfo.setDepartmentId(graduate.getCollegeId() == null ? null : graduate.getCollegeId());
        studentInfo.setMajor(graduate.getMajorName());
        studentInfo.setMajorId(graduate.getMajorId());
        studentInfo.setClassName(graduate.getClassesName());
        studentInfo.setClassId(graduate.getClassesId());
        studentInfo.setIdCard(graduate.getIdCard());
        studentInfo.setGraduateYear(graduate.getGyear());
        studentInfo.setEduBackground(graduate.getEducation());
        return studentInfo;
    }

    @Override
    public TypeUserInfo getTypeUserInfo(Long userId) {
        Graduate graduateOne = graduateService.selectGraduateById(userId);
        if (graduateOne == null) {
            throw new ServiceException("无效的学生ID");
        }
        SysUser sysUser = userService.selectUserByUserName(graduateOne.getStuNo());
        if (sysUser == null) {
            throw new ServiceException("无效的学生ID");
        }
        return getStudentInfo(graduateOne, sysUser);
    }
}
